﻿using PoemService.Shared;
using System.Data;
using System.Reflection;

namespace PoemGame.PoemDataXml
{
    public class PoemServiceXml : IPoemService
    {
        private readonly DataTable poemlinedt;
        private readonly DataTable poetdt;

        public PoemServiceXml()
        {
            var ds = new DataSet();
            string resourceName = @"PoemService.Xml.Data.PoemLine.Xml";
            Assembly someAssembly = Assembly.GetExecutingAssembly();
            using (Stream resourceStream = someAssembly.GetManifestResourceStream(resourceName))
            {
                ds.ReadXml(resourceStream);
                poemlinedt = ds.Tables[0];
            }
            var ds1 = new DataSet();
            string resourceName1 = @"PoemService.Xml.Data.Poet.Xml";
            Assembly someAssembly1 = Assembly.GetExecutingAssembly();

            using (Stream resourceStream1 = someAssembly1.GetManifestResourceStream(resourceName1))
            {
                ds1.ReadXml(resourceStream1);
                poetdt = ds1.Tables[0];
            }

        }
        public async Task<bool> IsPoemLineExist(string line)
        {
            var rows = poemlinedt.Select("LineContent='" + line + "'");
            return await Task.FromResult(rows.Count() > 0);
        }

        public async Task<PoemLine> GetPoemLine(string line)
        {
            var rows = poemlinedt.Select("LineContent='" + line + "'");
            if (rows.Count() > 0)
            {
                var poemLine = new PoemLine();
                poemLine.PoemLineId = rows[0]["PoemLineId"].ToString();
                poemLine.PoemId = rows[0]["PoemId"].ToString();
                poemLine.LineContent = rows[0]["LineContent"].ToString();
                poemLine.Order = int.Parse(rows[0]["Order"].ToString());
                return await Task.FromResult(poemLine);
            }
            return null;

        }

        public async Task<List<PoemLine>> GetPoemLineByPoemId(string poemId)
        {
            var rows = poemlinedt.Select("PoemId='" + poemId + "'", "Order asc");
            var lst = new List<PoemLine>();
            foreach (var row in rows)
            {
                var poemLine = new PoemLine();
                poemLine.PoemLineId = row["PoemLineId"].ToString();
                poemLine.PoemId = row["PoemId"].ToString();
                poemLine.LineContent = row["LineContent"].ToString();
                poemLine.Order = int.Parse(row["Order"].ToString());
                lst.Add(poemLine);
            }
            return await Task.FromResult(lst.OrderBy(o => o.Order).ToList());
        }

        public async Task<Poet> GetPoetByName(string name)
        {
            var rows = poetdt.Select("Name='" + name + "'");
            if (rows.Count() > 0)
            {
                var poet = new Poet
                {
                    PoetID = rows[0]["PoetID"].ToString(),
                    Name = rows[0]["Name"].ToString(),
                    Description = rows[0]["Description"].ToString()
                };
                return await Task.FromResult(poet);
            }
            return null;
        }
    }
}